package com.qiyou.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;

import com.qiyou.dao.IUserInfoDao;
import com.qiyou.dao.factory.DaoFactory;
import com.qiyou.entity.UserInfo;
import com.qiyou.util.LogUtil;

/**
 * 个人信息服务
 */
@WebServlet("/UserInfoService")
public class UserInfoService extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final Logger log = LogUtil.getLogger(UserInfoService.class);

    public UserInfoService() {
        super();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");

        IUserInfoDao dao = DaoFactory.getUserInfoDao();
        String userID = request.getParameter("id");
        UserInfo userInfo = null;
        try {
            userInfo = dao.getUserInfo(userID);
        } catch (SQLException e) {
            log.error(e);
        }
        JSONArray questionjson = new JSONArray();
        if (userInfo != null) {
            questionjson.put(new JSONObject(userInfo));
        }

        String msg = questionjson.toString();
        PrintWriter out = response.getWriter();
        out.print(msg);
        out.flush();
        out.close();

        log.info("用户:" + userID + ",访问了个人信息:" + msg);
    }

}
